<template>
  <el-dialog
    :title="!dataForm.rotationId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="栏目ID" prop="columnName">
<!--      <el-input v-model="dataForm.columnName" placeholder=""></el-input>-->
      <el-input v-model="dataForm.columnId" @blur="addRotation()"></el-input>
    </el-form-item>
      <el-form-item label="栏目名称" prop="columnName">
        <el-input v-model="dataForm.columnName" placeholder="填入栏目名称自动填写" id="columnName"></el-input>
      </el-form-item>
      <el-form-item label="栏目界面" prop="rotationAddr">
        <el-input v-model="dataForm.rotationAddr" placeholder="填入栏目名称自动填写,也可自行修改"></el-input>
      </el-form-item>
<!--      <el-form-item-->
<!--        prop="rotationAddr"-->
<!--        header-align="center"-->
<!--        align="center"-->
<!--        width="150"-->
<!--        label="界面展示">-->
<!--        <template slot-scope="scope">-->
<!--          <img :src="scope.row.rotationAddr" style="width: 100px; height: 80px"/>-->
<!--        </template>-->
<!--      </el-form-item>-->
    <el-form-item
      prop="isRotation"
      header-align="center"
      label="是否轮播">
<!--      <el-input v-model="dataForm.sort" placeholder=""></el-input>-->

<!--      <template slot-scope="scope">-->
        <el-switch
          v-model="dataForm.isRotation"
          active-color="#13ce66"
          inactive-color="#ff4949"
          :active-value="1"
          :inactive-value="0"
         >
        </el-switch>
<!--      </template>-->
    </el-form-item>
    <el-form-item label="次序" prop="sort">
      <el-input v-model="dataForm.sort" placeholder=""></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
      <el-button @click="visible = false">取消</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          columnId:'',
          columnName:'',
          rotationAddr: '',
          sort: '',
          isRotation:''
        },
        dataRule: {
          columnName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          rotationAddr: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          sort: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ]
        },

      }
    },
    methods: {
        // chooseParentMenu() {
        //     this.$http({
        //         url: this.$http.adornUrl('/generator/rotation/listTree'),
        //         method: 'get'
        //     }).then(({data}) => {
        //         console.log(data.data)
        //         this.menuForm = data.data
        //
        //     })
        // },
      init (id) {
          // this.chooseParentMenu()
        this.dataForm.rotationId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.rotationId) {
            this.$http({
              url: this.$http.adornUrl(`/generator/rotation/info/${this.dataForm.rotationId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                console.log(data);
                this.dataForm.columnId = data.rotation.columnId
                this.dataForm.rotationAddr = data.rotation.rotationAddr
                this.dataForm.sort = data.rotation.sort
                this.dataForm.isRotation = data.rotation.isRotation
                this.dataForm.columnName = data.rotation.columnName
              }
            })
          }
        })
      },
      //提交columnId,回显内容
      addRotation (){
        this.$http({
            url: this.$http.adornUrl('/generator/rotation/getOneData'),
            method: 'post',
            data:{'columnId':this.dataForm.columnId}
        }).then(({data}) => {
          if(data.code==500){
            console.log(12);
            this.dataForm.columnId = ""
            this.$message.error(data.msg)
          }
          this.dataForm=data.oneRotaionData
        })
      },
      // 新增
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/rotation/${!this.dataForm.rotationId ? 'insert' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'rotationId': this.dataForm.rotationId || undefined,
                'columnId': this.dataForm.columnId,
                'rotationAddr': this.dataForm.rotationAddr,
                'sort': this.dataForm.sort,
                'isRotation': this.dataForm.isRotation,
                'columnName': this.dataForm.columnName
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>

<style>
  .el-form-item__label:before{
    display: none!important;
  }
</style>
